const fs = require('fs');
const { resolve } = require('path');

const testPath = './src/test/';
const examplesPath = './src/examples/';
const examples__Path = './src/examples__/';
const threeJSPath = './src/threeJS/';

// 获取文件夹的子文件或者子文件夹
function getFileChild(path) {
  const files = fs.readdirSync(path);
  const fileArray = [];
  files.forEach((value) => {
    const stat = fs.lstatSync(`${path}\\${value}`)
      .isDirectory();
    if (stat && (value === 'css' || value === 'js' || value === 'fonts') || value === 'static') return; // console.warn('isDirectory', value)
    if (value.indexOf('Test.html') === -1) {
      fileArray.push(value);
    }
  });
  return fileArray;
}

// 获取 threeJS 文件下的树目录和需要打包的页面
function getThreePages() {
  const threeFileTree = {};
  const threePages = [];
  const fileChildPath = resolve(__dirname, threeJSPath);
  const fileChild = getFileChild(fileChildPath);
  fileChild.forEach((value) => {
    if (fs.lstatSync(`${fileChildPath}\\${value}`)
      .isDirectory()) {
      threeFileTree[value] = {};
      getFileChild(`${fileChildPath}\\${value}\\html`)
        .forEach((val) => {
          threeFileTree[value][val] = val;
          threePages.push({
            name: val.replace('.html', ''),
            entry: `${threeJSPath + value}/js/${val.replace('.html', '.js')}`,
            title: val.replace('.html', ''),
            template: `${threeJSPath}${value}/html/${val}`,
            filename: val,
            chunks: [val.replace('.html', '')],
          });
        });
    }
  });
  return {
    threePages,
    threeFileTree,
  };
}

// 获取 test examples 文件下的树目录和需要打包的页面
function getPages(filePath) {
  const fileTree = {};
  const pages = [];
  const fileChildPath = resolve(__dirname, filePath);
  const fileChild = getFileChild(fileChildPath+'\\html');
  fileChild.forEach((value) => {
    fileTree[value] = value;
    pages.push({
      name: value.replace('.html', ''),
      entry: `${filePath}js/${value.replace('.html', '.js')}`,
      title: value.replace('.html', ''),
      template: `${filePath}/html/${value}`,
      filename: value,
      chunks: [value.replace('.html', '')],
    });
  });

  return {
    pages,
    fileTree,
  };
}

const threePages = getThreePages();
const examplesPages = getPages(examplesPath);
const examples__Pages = getPages(examples__Path);
const testPages = getPages(testPath);
const pages = [
  ...threePages.threePages,
  ...examplesPages.pages,
  ...examples__Pages.pages,
  ...testPages.pages,
];
const trees = {
  threeJS: threePages.threeFileTree,
  examples: examplesPages.fileTree,
  examples__: examples__Pages.fileTree,
  test: testPages.fileTree,
};

// 创建index.html 索引素有的文件的目录
function createHtml() {
  let html = `<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>three.js学习笔记目录</title>
  <title>Title</title>
  <style>
    html,body{
      height: 100%;
      margin: 0;
    }
    h1,h2,h3,h4,h5,h6{
      margin: 0;
    }
    a {
      display: inline-block;
      background: rgba(0,0,0,0.1);
      padding: 5px;
      margin: 5px 0;
      color: #39f;
    }
    div>div{
      padding: 0 0 0 calc(2% + 15px);
    }
  </style>
</head>
<body>
<div>
<div>
`;

  function createList(tree, title) {
    let list = '<div>';
    ++title;
    Object.keys(tree)
      .forEach((key) => {
        if (typeof tree[key] === 'string') {
          list += `<a href="${key}">${key}</a><br/>\n`;
        } else {
          list += `\n\n<h${title}>${key}</h${title}>\n`;
          list += createList(tree[key], title);
        }
      });
    list += '</div>';
    return list;
  }

  html += createList(trees, 0);

  html += `
</body>
</html>
  `;
  fs.mkdir('../git-pages', (eer) => {
    fs.writeFile('../git-pages/index.html', html, (err) => {
      if (err) {
        console.error('three.js学习笔记目录创建失败', err);
      } else {
        console.log('three.js学习笔记目录创建成功');
      }
    });
  });
  return html;
}

createHtml();
module.exports = pages;
